package datastruct.document;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

import nlptools.NLPToolsFactory;
import nlptools.enju.EnjuParser;
import nlptools.opennlp.OpenNLPParser;

public class BiomedDocument {
	private static EnjuParser enjuParser = null;
	private static OpenNLPParser openNLPParser = null;
	private String[] sentenceArray = null;
	private String[] syntaxTreeStrArray = null;
	private char[] sentenceTagArray = null;
	public void ReadFromRawTextFile(String filename)
	{
		if(enjuParser==null) enjuParser = NLPToolsFactory.getEnjuParser();
		if(openNLPParser==null) openNLPParser = NLPToolsFactory.getOpenNLPParser();
		try {
			BufferedReader freader = new BufferedReader(new FileReader(filename));
			String line;
			String wholeStr = "";
			while((line=freader.readLine())!=null)
			{
				wholeStr += line + " ";
			}
			freader.close();
			this.sentenceArray = openNLPParser.getSentence(wholeStr);
			int size = this.sentenceArray.length;
			this.syntaxTreeStrArray = new String[size];
			this.sentenceTagArray = new char[size];
			for(int i=0;i<size;i++)
			{
				this.syntaxTreeStrArray[i] = "";
				this.sentenceTagArray[i] = 'N';
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void initNLPTools()
	{
		enjuParser = NLPToolsFactory.getEnjuParser();
		openNLPParser = NLPToolsFactory.getOpenNLPParser();
	}
	public void deepParseAllSentence()
	{
		if(this.sentenceArray==null) return;
		if(enjuParser==null) enjuParser = NLPToolsFactory.getEnjuParser();
		if(openNLPParser==null) openNLPParser = NLPToolsFactory.getOpenNLPParser();
		for(int i=0;i<this.sentenceArray.length;i++)
		{
			this.syntaxTreeStrArray[i] = enjuParser.ParserSentenceToXml(this.sentenceArray[i]);
		}
	}
	public void deepParseTaggedSentence()
	{
		if(this.sentenceArray==null) return;
		if(enjuParser==null) enjuParser = NLPToolsFactory.getEnjuParser();
		if(openNLPParser==null) openNLPParser = NLPToolsFactory.getOpenNLPParser();
		for(int i=0;i<this.sentenceArray.length;i++)
		{
			if(this.sentenceTagArray[i]=='E')
				this.syntaxTreeStrArray[i] = enjuParser.ParserSentenceToXml(this.sentenceArray[i]);
		}
	}
	public void WriterToParsedTextFile(String filename)
	{
		try {
			BufferedWriter fwriter = new BufferedWriter(new FileWriter(filename));
			for(int i=0;i<this.sentenceArray.length;i++)
			{
				fwriter.write("#S"+i+":"+this.sentenceTagArray[i]);
				fwriter.newLine();
				fwriter.write(this.sentenceArray[i]);
				fwriter.newLine();
				fwriter.write(this.syntaxTreeStrArray[i]);
				fwriter.newLine();
				fwriter.newLine();
			}
			fwriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void ReadFromParsedFile(String filename)
	{
		try {
			BufferedReader freader = new BufferedReader(new FileReader(filename));
			String line;
			Vector<String> sentenceVector = new Vector<String>();
			Vector<String> sytaxtreeVector = new Vector<String>();
			Vector<Character> tagVector = new Vector<Character>();
			while((line=freader.readLine())!=null)
			{
				if(line.startsWith("#S"))
				{
					int pos = line.indexOf(':');
					tagVector.add(line.substring(pos+1).charAt(0));
					line = freader.readLine();
					sentenceVector.add(line);
					line = freader.readLine();
					sytaxtreeVector.add(line);
				}
			}
			freader.close();
			int size = sentenceVector.size();
			this.sentenceArray = new String[size];
			this.syntaxTreeStrArray = new String[size];
			this.sentenceTagArray = new char[size];
			for(int i=0;i<size;i++)
			{
				this.sentenceArray[i] = sentenceVector.elementAt(i);
				this.syntaxTreeStrArray[i] = sytaxtreeVector.elementAt(i);
				this.sentenceTagArray[i] = tagVector.elementAt(i);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public String[] getSentences()
	{
		return this.sentenceArray.clone();
	}
	public String[] getSyntaxTrees()
	{
		return this.syntaxTreeStrArray.clone();
	}
	public char[] getSentenceTags()
	{
		return this.sentenceTagArray;
	}
	public String getSenetence(int index)
	{
		return this.sentenceArray[index];
	}
	public String getSyntaxTree(int index)
	{
		return this.syntaxTreeStrArray[index];
	}
	public int getSize()
	{
		if(this.sentenceArray==null) return 0;
		return this.sentenceArray.length;
	}
}
